<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 福鑫2
 */
class Fuxin2Controller extends PayController{
    protected $config;
	protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));

        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['sdk'] = $this->config['mch_id'];
        $data['record'] = $return['orderid'];
        $data['money'] = number_format($return['amount'],2,'.','');
        $data['refer'] = $return['callbackurl'];
        //$data['type'] = 'json';
        $data['notify_url'] = $return['notifyurl'];
        $data['sign'] = $this->getSign($data,$this->config['mch_id']);

        po_get($this->config['gateway'], $data);
    }

    public function getSign ($data,$key){
        $str = number_format($data['money'], 2, '.','') . trim($data['record']) . $key;
        $sign = md5($str);
        return $sign;
    }

    //通知
    public function notifyurl(){
		//$this->debug_callback();
        $p = $origin = I("post.");
		
		/*$p = array (
            'money' => '10.00',
            'amount' => '10.00',
            'order' => '20190306181245155186716586632',
            'record' => '2019030618124980116',
            'remark' => '03065624209500070951',
            'attach' => '[attach]',
            'sign' => 'e9a9a1a4d1764ad75767bfd76df45268',
        );*/

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['record'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['order'])
        ));

        //获取渠道信息
        $this->config = Channel::getInstance()->getById($find['channel_id']);

        $sign = $p['sign'];

        $sign2 = $this->getSign($p,$this->config['signkey']);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        //处理支付
        $this->async($find['pay_orderid'],'success');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}